package B刷题.暴力递归.A回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wei.zhao
 * @description: 17.电话号码的字母组合
 * @date 2022年04月10日 11:31
 */
public class Code04_LetterCombinations {

    /**
     * 数字对应的字母集，可以使用HashMap维护，但数组也能维护两个信息，而且更快所以使用数组。
     */
    public static final String[][] map = new String[][]{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}, {"m", "n", "o"}, {"p", "q", "r", "s"}, {"t", "u", "v"}, {"w", "x", "y", "z"}};

    ArrayList<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (null == digits || digits.length() == 0) {
            return result;
        }
        process(digits.toCharArray(), "");
        return result;
    }

    private void process(char[] chars, String s) {
        if (s.length() == chars.length) {
            result.add(s);
            return;
        }
        char c = chars[s.length()];
        String[] strings = map[c - '2'];
        for (String string : strings) {
            process(chars, s + string);
        }
    }

}
